/*
 * AddElementToWorldMouseListener.cpp
 *
 *  Created on: 07-01-2011
 *      Author: Przemek
 */

#include "listener/AddElementToWorldMouseListener.hpp"

AddElementToWorldMouseListener::AddElementToWorldMouseListener(const Checkbox* addOnDragCheckbox,
		const Checkbox* anchorToGridCheckbox, CreationState* creationState) :
	addOnDragCheckbox(addOnDragCheckbox), anchorToGridCheckbox(anchorToGridCheckbox), creationState(
			creationState)
{
}

void AddElementToWorldMouseListener::mouseReleased(const MouseListener::EventArguments& arguments)
{
	if(arguments.getButton() == MouseListener::EventArguments::RIGHT)
	{
		creationState->clearCreateTool();
	}
	else if(creationState->anyToAdd())
	{
		creationState->tryAddElementToWorld();
	}
}

void AddElementToWorldMouseListener::mouseDragged(const MouseListener::EventArguments& arguments)
{
	mouseMoved(arguments);
	if(addOnDragCheckbox->isChecked() && creationState->anyToAdd())
	{
		creationState->tryAddElementToWorld();
	}
}

void AddElementToWorldMouseListener::mouseMoved(const MouseListener::EventArguments& arguments)
{
	if(creationState->anyToAdd())
	{
		const Vector& point = arguments.getPoint();

		if(anchorToGridCheckbox->isChecked())
		{
			creationState->moveAnchoredToGrid(point);
		}
		else
		{
			creationState->moveCenteredTo(point);
		}
	}
}
